package com.eduction.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.eduction.modules.system.dao.QuestionnaireSurveyRepository;
import com.eduction.modules.system.entity.QuestionnaireSurvey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eduction.common.dbutil.page.Page;
import com.eduction.modules.system.dao.SchoolRepository;
import com.eduction.modules.system.entity.CityEntity;
import com.eduction.modules.system.entity.School;
import com.eduction.modules.system.entity.SchoolType;
import com.eduction.service.SchoolService;

@Service
public class SchoolServiceImpl implements SchoolService {

	@Autowired
	private SchoolRepository schoolRepository;
	@Autowired
	QuestionnaireSurveyRepository questionnaireSurveyRepository;
	
	@Override
	public School querySchoolByAccount(String account) throws SQLException {
		School s = schoolRepository.querySchoolByAccount(account);
		return s;
	}

	@Override
	public void saveSchool(School s) throws SQLException {

		schoolRepository.saveSchool(s);
		
	}

	@Override
	public void updateState(School s) throws SQLException {

		schoolRepository.updateState(s);

	}



	@Override
	public void resetPassword(String id,String password) throws SQLException {
		schoolRepository.resetPassword(id,password);
	}

		@Override
	public Page<School> querySchoolByPage(Map<String, String> map) throws SQLException {
		Page<School> page = schoolRepository.querySchoolByPage(map);
		return page;
	}

	@Override
	public List<School> querySchoolByYear(String year) throws SQLException {
		List<School> list = schoolRepository.querySchoolByYear(year);
		return list;
	}



	@Override
	public List<CityEntity> queryCities() throws SQLException {
		List<CityEntity> cities = schoolRepository.queryCities();
		return cities;
	}

	@Override
	public List<SchoolType> querySchoolType() throws SQLException {
		List<SchoolType> types = schoolRepository.querySchoolType();
		return types;
	}



	@Override
	public void deleteSchool(String pid) throws SQLException {
		schoolRepository.deleteSchool(pid);
	}

	@Override
	public List<School> querySchoolNotBand() throws SQLException {
		
		return this.schoolRepository.querySchoolNotBand();
	}

	@Override
	public List<School> querySchoolNotBand2() throws SQLException {

		return this.schoolRepository.querySchoolNotBand2();
	}

	@Override
	public List<School> querySchoolByExpert(String expertId) throws SQLException {
		// TODO Auto-generated method stub
		return this.schoolRepository.querySchoolByExpert(expertId);
	}

	@Override
	public List<School> querySchoolByExpertOne(String expertId) throws SQLException {
		// TODO Auto-generated method stub
		return this.schoolRepository.querySchoolByExpertOne(expertId);
	}

	@Override
	public School queryNameById(String id) throws SQLException {
		// TODO Auto-generated method stub
		return this.schoolRepository.queryNameById(id);
	}

	@Override
	public School querySchoolById(String id) throws SQLException {
		School s = schoolRepository.querySchoolById(id);
		return s;
	}

	@Override
	public School queryById(String id) throws SQLException {
		// TODO Auto-generated method stub
		return this.schoolRepository.queryById(id);
	}

	@Override
	public List<School> queryBySchoolType(String schoolType) throws SQLException {
		if("0".equals(schoolType)){
			schoolType="1495071155575000";
		}else if("1".equals(schoolType)){
			schoolType="1495071155575001";
		}
		return this.schoolRepository.queryBySchoolType(schoolType);
	}

	@Override
	public void resetSchoolStateBySchool(String schoolId) throws SQLException{
		schoolRepository.resetSchoolStateBySchool(schoolId);
	}


	@Override
	public void saveQuestionnaireSurvey(QuestionnaireSurvey qs) throws SQLException{
		QuestionnaireSurvey qs2= questionnaireSurveyRepository.findbySchoolId(qs.getSchool_id());
		if(qs2!=null){
			if(qs.getTb_code_url()!=null) {
				qs2.setTb_code_url(qs.getTb_code_url());
			}
			if(qs.getTb_code_result_url()!=null){
				qs2.setTb_code_result_url(qs.getTb_code_result_url());
			}
			questionnaireSurveyRepository.save(qs2);
		}else{
			questionnaireSurveyRepository.save(qs);
		}
	}

	@Override
	public QuestionnaireSurvey getTbCodeUrl(String schoolId) throws SQLException{
		return questionnaireSurveyRepository.findbySchoolId(schoolId);
	}


}
